package com.yb.forum.controller;

import com.yb.forum.common.AppResult;
import com.yb.forum.model.Board;
import com.yb.forum.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "板块接口")
@Slf4j
@RestController
@RequestMapping("/board")
public class BoardController {

    // 从yml文件中读取配置,若没有配置,则默认值为9
    @Value("${yb-forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Resource
    private IBoardService iBoardService;
    @ApiOperation("获取首页板块列表")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        log.info("配置首页的板块个数为: "+ indexBoardNum);
        // 调用service查询结果
        List<Board> boards = iBoardService.selectByNum(indexBoardNum);
        // 为空需要转成List数据,以用于json序列化
        if(boards == null){
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }


}
